package com.sp.scmdb.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sp.scmdb.bean.ShiftsBean;
import com.sp.scmdb.bean.UserBean;
import com.sp.scmdb.common.CommonParamter;
import com.sp.scmdb.dao.EmployeeDao;
import com.sp.scmdb.dao.LeaderShipDao;
import com.sp.scmdb.dao.ShiftsDao;
import com.sp.scmdb.dao.ShiftsDetailsDao;
import com.sp.scmdb.entity.LeaderShip;
import com.sp.scmdb.entity.Shifts;
import com.sp.scmdb.entity.ShiftsDetails;
import com.sp.scmdb.service.LogService;
import com.sp.scmdb.service.ShiftsService;

@Service("shiftsService")
public class ShiftsServiceImpl implements ShiftsService {

	private ShiftsDao shiftsDao;
	private EmployeeDao empDao;
	private ShiftsDetailsDao shiftsDetailsDao;
	private LogService logs;
	private LeaderShipDao leaderShipDao;

	public void setLeaderShipDao(LeaderShipDao leaderShipDao) {
		this.leaderShipDao = leaderShipDao;
	}

	public void setLogs(LogService logs) {
		this.logs = logs;
	}

	public void setShiftsDetailsDao(ShiftsDetailsDao shiftsDetailsDao) {
		this.shiftsDetailsDao = shiftsDetailsDao;
	}

	public void setShiftsDao(ShiftsDao shiftsDao) {
		this.shiftsDao = shiftsDao;
	}

	public void setEmpDao(EmployeeDao empDao) {
		this.empDao = empDao;
	}

	@Transactional(readOnly = true)
	public List<ShiftsBean> queryAll() {
		List<Shifts> list = shiftsDao.queryAll();
		Shifts shifts = null;
		ShiftsBean bean = null;
		List<ShiftsDetails> listDetails = null;
		List<ShiftsBean> listShiftBean = new ArrayList<ShiftsBean>();
		StringBuffer shiftsDetails = null;
		for (int i = 0; i < list.size(); i++) {
			shifts = list.get(i);
			bean = new ShiftsBean();
			bean.setShiftId(shifts.getId().toString());
			bean.setName(shifts.getName());
			listDetails = shiftsDetailsDao
					.queryHql("from ShiftsDetails where idShifts = "
							+ bean.getShiftId());
			shiftsDetails = new StringBuffer("");
			for (int j = 0; j < listDetails.size(); j++) {
				shiftsDetails.append(listDetails.get(j).getBeginTime() + "~"
						+ listDetails.get(j).getEndTime() + ",");
			}
			bean.setShiftsDetails(shiftsDetails.toString());
			listShiftBean.add(bean);
		}

		return listShiftBean;
	}

	@Transactional
	public String newShifts(ShiftsBean bean, UserBean user) {
		String result = "3";
		if (StringUtils.isNotEmpty(bean.getName())) {
			StringBuffer Hql = new StringBuffer("from Shifts where name='"
					+ bean.getName() + "'");
			List<Shifts> list = shiftsDao.queryHql(Hql.toString());
			if (CollectionUtils.isEmpty(list)) {
				// 首先添加班制信息(OBJ_SHIFTS)
				Shifts shifts = new Shifts();
				shifts.setName(bean.getName());
				shifts.setIsCrossDay(bean.getIsCorssDay());
				shiftsDao.save(shifts);

				// 然后添加详细班次信息
				ShiftsDetails shiftsDetails = null;
				String[] detaisNames = bean.getDetail().split(",");
				String[] beginTimeHour = bean.getStartTime().split(",");
				String[] endTimeHour = bean.getEndTime().split(",");
				for (int i = 0; i < detaisNames.length; i++) {
					shiftsDetails = new ShiftsDetails();
					shiftsDetails.setIdShifts(shifts.getId());
					shiftsDetails.setName(detaisNames[i].trim());
					shiftsDetails.setBeginTime(beginTimeHour[i].trim()
							.substring(0, 5));
					if ("00:00".equals(endTimeHour[i].trim())) {
						// 如果结束时间是“00:00”，则修改为“23:59”
						shiftsDetails.setEndTime("23:59");
					} else {
						shiftsDetails.setEndTime(endTimeHour[i].trim()
								.substring(0, 5));
					}
					shiftsDetailsDao.save(shiftsDetails);
				}
				logs.addLog(user, "添加 班次\"" + bean.getName() + "\"成功！");

				result = CommonParamter.CHECK_SUCCESS;
			} else {
				result = CommonParamter.CHECK_REPEAT;
			}
		}
		return result;
	}

	@Transactional
	public String editShifts(ShiftsBean bean, UserBean user) {
		String result = "2";
		// 检查班制名称是否重复
		if (StringUtils.isNotEmpty(bean.getName())) {
			StringBuffer Hql = new StringBuffer();
			Hql.append("from Shifts where id != " + bean.getShiftId()
					+ " and name = '" + bean.getName() + "'");
			if (CollectionUtils.isEmpty(shiftsDao.queryHql(Hql.toString()))) {
				Shifts shifts = shiftsDao.getById(Integer.parseInt(bean
						.getShiftId()));
				// 如果名称没有重复的
				shifts.setName(bean.getName());
				shifts.setIsCrossDay(bean.getIsCorssDay());
				// 保存该班制
				shiftsDao.update(shifts);

				// 删除详细班次(OBJ_SHIFTS_DETAILS)
				List<ShiftsDetails> list = shiftsDetailsDao
						.queryHql("from ShiftsDetails where idShifts = "
								+ shifts.getId());
				ShiftsDetails shiftsDetails = null;
				// saveOrUpdate详细班次(OBJ_SHIFTS_DETAILS)
				String[] detaisNames = bean.getDetail().split(",");
				String[] beginTimeHour = bean.getStartTime().split(",");
				String[] endTimeHour = bean.getEndTime().split(",");
				for (int i = 0; i < detaisNames.length; i++) {
					if (i < list.size()) {
						shiftsDetails = list.get(i);	
					} else {
						shiftsDetails = new ShiftsDetails();
					}
					
					shiftsDetails.setIdShifts(shifts.getId());
					shiftsDetails.setName(detaisNames[i].trim());
					shiftsDetails.setBeginTime(beginTimeHour[i].trim()
							.substring(0, 5));
					if ("00:00".equals(endTimeHour[i].trim())) {
						// 如果结束时间是“00:00”，则修改为“23:59”
						shiftsDetails.setEndTime("23:59");
					} else {
						shiftsDetails.setEndTime(endTimeHour[i].trim()
								.substring(0, 5));
					}
					shiftsDetailsDao.saveOrUpdate(shiftsDetails);
				}

				logs.addLog(user, "修改 班次\"" + bean.getName() + "\"成功！");
				result = CommonParamter.CHECK_SUCCESS;
			} else {
				result = CommonParamter.CHECK_REPEAT;
			}
		}
		return result;
	}

	@Transactional
	public String delShifts(String[] ids, UserBean user) {
		String result = "1";
		Shifts shifts = null;
		for (int i = 0; i < ids.length; i++) {
			shifts = shiftsDao.getById(Integer.parseInt(ids[i]));
			if (shifts != null) {
				// 如果该班制已经有所属人员，则不能删除
				if (CollectionUtils.isNotEmpty(empDao
						.queryHql("from Employee where empShiftID="
								+ shifts.getId()))) {
					return "10";
				}
				logs.addLog(user, "删除 班次：\"" + shifts.getName() + "\"成功！");
				// 删除详细班次(OBJ_SHIFTS_DETAILS)
				List<ShiftsDetails> list = shiftsDetailsDao
						.queryHql("from ShiftsDetails where idShifts = "
								+ shifts.getId());
				ShiftsDetails shiftsDetails = null;
				for (int j = 0; j < list.size(); j++) {
					shiftsDetails = list.get(j);
					shiftsDetailsDao.delete(shiftsDetails);
				}
				// 删除班制信息
				shiftsDao.delete(shifts);

				result = CommonParamter.CHECK_SUCCESS;
			}
		}
		return result;
	}

	@Transactional(readOnly = true)
	public ShiftsBean findID(String id) {
		// 获取班制
		Shifts shifts = shiftsDao.getById(Integer.parseInt(id));
		// 获取班制所属详细版次信息
		List<ShiftsDetails> list = shiftsDetailsDao
				.queryHql("from ShiftsDetails where idShifts = " + id);

		ShiftsBean bean = new ShiftsBean();
		bean.setShiftId(shifts.getId().toString());
		bean.setName(shifts.getName());
		bean.setIsCorssDay(shifts.getIsCrossDay());
		bean.setListShiftsDetails(list);

		return bean;
	}

	@Override
	public List<LeaderShip> queryLeadership(String shiftsDetailID) {
		List<LeaderShip> leaderships = leaderShipDao
				.queryHql("from LeaderShip where shiftsDetailID="
						+ shiftsDetailID);
		return leaderships;
	}

	@Override
	@Transactional
	public String delShiftsDetail(String shiftsDetailID) {
		String result = CommonParamter.CHECK_SUCCESS;
		try {
			ShiftsDetails shiftsDetails = shiftsDetailsDao.getById(Integer
					.parseInt(shiftsDetailID));
			shiftsDetailsDao.delete(shiftsDetails);
		} catch (Exception e) {
			
		}
		return result;
	}
}
